//
// Created by mac on 2023/12/2.
//

// 二叉树前序遍历

// 求二叉树的最大深度
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
private:
    TreeNode* _buildTree(vector<int>& preorder, vector<int>& inorder, int& prei, int inBegin, int inEnd)
    {
        // 特殊情况处理
        if(inBegin > inEnd) return nullptr;
        // 构造根节点
        TreeNode* root = new TreeNode(preorder[prei]);
        // 找到中序序列中根节点的位置
        int rooti = inBegin;
        while(inorder[rooti] != preorder[prei]) ++rooti;
        // 划分中序序列区间 [inBegin, rooti-1] rooti [rooti+1, inEnd]
        // 根据区间构造左、右子树
        ++prei;
        root->left = _buildTree(preorder, inorder, prei, inBegin, rooti - 1);
        root->right = _buildTree(preorder, inorder, prei, rooti + 1, inEnd);
        // 返回根节点
        return root;
    }

public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
    {
        int prei = 0, inBegin = 0, inEnd = inorder.size() - 1;
        return _buildTree(preorder, inorder, prei, inBegin, inEnd);
    }
};

// 从前序与中序遍历序列构造二叉树
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
private:
    TreeNode* _buildTree(vector<int>& preorder, vector<int>& inorder, int& prei, int inBegin, int inEnd)
    {
        // 特殊情况处理
        if(inBegin > inEnd) return nullptr;
        // 构造根节点
        TreeNode* root = new TreeNode(preorder[prei]);
        // 找到中序序列中根节点的位置
        int rooti = inBegin;
        while(inorder[rooti] != preorder[prei]) ++rooti;
        // 划分中序序列区间 [inBegin, rooti-1] rooti [rooti+1, inEnd]
        // 根据区间构造左、右子树
        ++prei;
        root->left = _buildTree(preorder, inorder, prei, inBegin, rooti - 1);
        root->right = _buildTree(preorder, inorder, prei, rooti + 1, inEnd);
        // 返回根节点
        return root;
    }

public:
    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
    {
        int prei = 0, inBegin = 0, inEnd = inorder.size() - 1;
        return _buildTree(preorder, inorder, prei, inBegin, inEnd);
    }
};

// 二叉树前序遍历
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
    vector<int> preorderTraversal(TreeNode* root)
    {
        // 特殊情况处理
        if(!root) return {};
        // 0、初始化
        vector<int> ans;
        stack<TreeNode*> st;
        // 1、访问左路节点，并把访问好的左路节点入栈
        TreeNode* cur = root;
        while(cur)
        {
            st.push(cur);
            ans.push_back(cur->val);
            cur = cur->left;
        }
        // 2、取栈中已经访问完成的节点，去访问它的右子树
        while(!st.empty())
        {
            cur = st.top()->right;
            st.pop();

            while(cur)
            {
                st.push(cur);
                ans.push_back(cur->val);
                cur = cur->left;
            }
        }
        // 3、返回值
        return ans;
    }
};